// random/pages/place_order/place_order.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        product_id: "",
        product_item_id: "",
        bill_id: "",
        receiving_address_name: "",
        receiving_name: "",
        receiving_phone: "",
        receiving_detail: "",
        receiving_address_name: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            product_item_id: options.product_item_id || "",
            product_id: options.id || "",
            bill_id: options.bill_id || ""
        });
        this.getInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    recordValue: function(e) {
        let key = e.currentTarget.dataset.name;
        let value = e.detail.value;
        console.log(key);
        this.setData({
            [key]: value
        });
    },
    chooseArea: function(e) {
        this.setData({
            receiving_address_name: e.detail.value
        });
    },
    remind: function() {
        let that = this;
        let {product_id: product_id, product_item_id: product_item_id, bill_id: bill_id, receiving_address_name: receiving_address_name, receiving_name: receiving_name, receiving_phone: receiving_phone, receiving_detail: receiving_detail} = that.data;
        if (!receiving_name) {
            wx.showModal({
                title: "提示",
                content: "请输入收件人！",
                showCancel: false
            });
            return false;
        }
        if (!/^[1][0-9]{10}$/.test(that.data.receiving_phone)) {
            wx.showModal({
                title: "提示",
                content: "请输入正确的手机号码！",
                showCancel: false
            });
            return false;
        }
        if (!receiving_detail) {
            wx.showModal({
                title: "提示",
                content: "请输入详细地址！",
                showCancel: false
            });
            return false;
        }
        common.request({
            endpoint: "/Random/manage/place-order",
            method: "post",
            data: {
                product_id: product_id,
                product_item_id: product_item_id,
                bill_id: bill_id,
                receiving_address_name: receiving_address_name.join("-"),
                receiving_name: receiving_name,
                receiving_phone: receiving_phone,
                receiving_detail: receiving_detail
            },
            header: {
                "content-type": "application/json"
            },
            success(res) {
                wx.showToast({
                    icon: "none",
                    title: "领取成功,等待奖品发放",
                    duration: 3e3
                });
                var if_share = res.extra.if_share;
                var share_detail = res.extra.share_detail;
                if (if_share == 1) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 0,
                            success() {
                                prevPage.setData({
                                    isReward: 0,
                                    alert1: 1,
                                    share_detail: share_detail
                                });
                            }
                        });
                    }, 3e3);
                } else {
                    setTimeout(() => {
                        wx.reLaunch({
                            url: "/random/pages/order/order"
                        });
                    }, 3e3);
                }
                //  setTimeout(()=>{ 
                //    wx.reLaunch({
                //      url: '/pages/index/index',
                //    })
                //  },3000)
                        },
            fail(res) {
                wx.showToast({
                    icon: "none",
                    title: res.message,
                    duration: 3e4
                });
            }
        });
    },
    getInfo: function() {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        common.request({
            endpoint: "member/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success(res) {
                console.log(res);
                that.setData({
                    receiving_address_name: res.extra.address_name,
                    receiving_name: res.extra.name,
                    receiving_phone: res.extra.phone,
                    receiving_detail: res.extra.address_other,
                    remark: res.extra.remark
                });
            }
        });
    }
});